package com.xls.box2023.tuiguangyouxi;

import com.lzz.bottomencapsulation.net.RequestCallBack;
import com.xls.box2023.Consts.IntentConsts;
import com.xls.box2023.adapter.TuiGuangAdapter;
import com.xls.box2023.entity.AppTeamUsersEntity;
import com.xls.box2023.entity.JiFenMingXiEntity;
import com.xls.box2023.mvp.BasePresenterImpl;
import com.xls.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import androidx.recyclerview.widget.RecyclerView;

/**
 * MVPPlugin
 */

public class TuiGuangYouXiPresenter extends BasePresenterImpl<TuiGuangYouXiContract.View> implements TuiGuangYouXiContract.Presenter{

    private String getAppId(){
        return activity.getIntent().getStringExtra(IntentConsts.id);
    }

    private String getPackageName(){
        return activity.getIntent().getStringExtra(IntentConsts.packageName);
    }

    public JiFenMingXiEntity getJiFenMingXi(){
        return (JiFenMingXiEntity) activity.getIntent().getSerializableExtra(IntentConsts.data);
    }


    @Override
    public void getSourceData() {
        super.getSourceData();
        TuiGuangYouXiUtils.getInstance().getAppTeamUsers(context, getAppId(), getPackageName(),getStartTime(),getStopTime(), new RequestCallBack<AppTeamUsersEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {
                if (activity==null||context==null)return;
                mView.getListView().refreshComplete();
                mView.getStatelayout().showContentView();
            }

            @Override
            public void onRequestSuccess(AppTeamUsersEntity requestData) {
                if (requestData==null||activity==null||context==null){
                    return;
                }
                userData.clear();
                if (requestData.getList()!=null&&requestData.getList().size()>0){
                    userData.addAll(requestData.getList());
                }
                tuiGuangAdapter.notifyDataSetChanged();
                if (userData.size()==0){
                    mView.getStatelayout().showEmptyView();
                }
//                long num=0;
//                for (AppTeamUsersEntity appTeamUsersEntity:userData){
//                    num=num+Long.parseLong(appTeamUsersEntity.getScoreTotal());
//                    if (appTeamUsersEntity.getList()!=null&&appTeamUsersEntity.getList().size()>0){
//                        long num2=0;
//                        for (AppTeamUsersEntity appTeamUsersEntity2:appTeamUsersEntity.getList()){
//                            num2=num2+Long.parseLong(appTeamUsersEntity2.getScoreTotal());
//                        }
//                        LUtils.e("团队总分"+appTeamUsersEntity.getUsername()+num2);
//                        num=num2+num;
//                    }
//                }
//                LUtils.e("团队总分"+num);
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {
                mView.getStatelayout().showErrorView(errmsg);
            }
        });
    }




    ArrayList<AppTeamUsersEntity> userData=new ArrayList<>();
    TuiGuangAdapter tuiGuangAdapter;
    @Override
    public RecyclerView.Adapter getAdapter() {
        if (tuiGuangAdapter==null){
            tuiGuangAdapter=new TuiGuangAdapter(context, userData);
            tuiGuangAdapter.setHasStableIds(true);
        }
        return tuiGuangAdapter;
    }

    @Override
    public XRecyclerView.LoadingListener getLoadingListener() {
        return new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mView.getStatelayout().showProgressView();
                getSourceData();
            }

            @Override
            public void onLoadMore() {

            }
        };
    }


    @Override
    public String getStartTime() {
        return activity.getIntent().getStringExtra(IntentConsts.StartTime);
    }

    @Override
    public String getStopTime() {
        return activity.getIntent().getStringExtra(IntentConsts.StopTime);
    }
}
